﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Hr.Entity;
using System.Collections;

namespace Chenxin.Lib.Hr.BLL
{
    /// <summary>
    /// 通讯录业务类
    /// </summary>
    public class Hr_Contacts
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Hr.Entity.Hr_Contacts model)
        {
            Chenxin.Lib.Hr.DAL.Hr_Contacts.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Hr.Entity.Hr_Contacts model)
        {
            Chenxin.Lib.Hr.DAL.Hr_Contacts.Update(model);
        }

        /// <summary>
        /// 获得实体
        /// </summary>
        public Chenxin.Lib.Hr.Entity.Hr_Contacts GetModel(Guid RecordId)
        {
            return Chenxin.Lib.Hr.DAL.Hr_Contacts.GetModel(RecordId);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid RecordId)
        {

            Chenxin.Lib.Hr.DAL.Hr_Contacts.Delete(RecordId);
        }

        public DataSet GetListByConditions(Hashtable htParams)
        {
            System.Text.StringBuilder StrWhere = new System.Text.StringBuilder();

            string OrderField = "UserName ASC";
            string FieldList = "RecordId,UserName,Company,Depart,Position,Mobile,Tel,Email";
            string TableFieldList = "RecordId,UserName,Company,Depart,Position,Mobile,Tel,Email";
            string TableName = "Hr_Contacts";
            int PageIndex = 1;
            int PageSize = 10;

            StrWhere.Append(" 1=1 ");
            //用户ID
            if (htParams.ContainsKey("RecordId"))
            {
                StrWhere.Append(" And RecordId='" + htParams["RecordId"] + "'");
            }
            //首字母
            if (htParams.ContainsKey("Initial"))
            {
                StrWhere.Append(" And Initial='" + htParams["Initial"] + "'");
            }

            if (htParams.ContainsKey("pageindex"))
            {
                PageIndex = Convert.ToInt32(htParams["pageindex"]);
            }
            if (htParams.ContainsKey("pageSize"))
            {
                PageSize = Convert.ToInt32(htParams["pageSize"]);
            }

            //根据sql执行查询
            DataSet ds = Chenxin.Lib.Hr.DAL.Hr_Contacts.GetList(PageIndex, PageSize, OrderField, TableName, StrWhere.ToString(), FieldList, TableFieldList, true);

            return ds;
        }

        public int GetCntOfContacts(Hashtable htParams)
        {
            System.Text.StringBuilder StrWhere = new System.Text.StringBuilder();
            string TableName = "Hr_Contacts";

            //string StrWhere = " 1=1 ";
            StrWhere.Append(" 1=1 ");
            //用户ID
            if (htParams.ContainsKey("UserId"))
            {
                StrWhere.Append(" And UserId='" + htParams["UserId"] + "'");
            }
            //首字母
            if (htParams.ContainsKey("Initial"))
            {
                StrWhere.Append(" And Initial='" + htParams["Initial"] + "'");
            }

            //根据sql执行查询
            DataSet ds = Chenxin.Lib.Hr.DAL.Hr_Contacts.GetList(0, 0, "", TableName, StrWhere.ToString(), "", "", false);

            //取得总记录数
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }
            else
            {
                return 0;
            }
        }

        public List<Chenxin.Lib.Hr.Entity.Hr_Contacts> GetListByQuery(string Query)
        {
            string strWhere;
            Query = Query.ToUpper();
            strWhere = "(Upper(UserName) like '%" + Query + "%')";
            strWhere = strWhere + " Or (Upper(Company) like '%" + Query + "%')";
            strWhere = strWhere + " Or (Upper(Depart) like '%" + Query + "%')";
            strWhere = strWhere + " Or (Upper(Position) like '%" + Query + "%')";

            return Chenxin.Lib.Hr.DAL.Hr_Contacts.GetListArray(strWhere);
        }
    }
}
